<%--
  Created by IntelliJ IDEA.
  User: notyoukennk
  Date: 2022/11/16
  Time: 21:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.sql.*" %>
<%@ page import="ProjectManagement.DobConn" %>
<%@ page language="java" import="java.sql.*"  contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<html>
<head>
  <title>留言板</title>
  <style>
    body{
      margin: 0;
    }
    .main_box{
      height: auto;
      width: 100%;
      background: linear-gradient(#5489d0, #ee61a8);
    }
    .submit_box{
      margin: auto;
      width: 70%;
      height: auto;
      display: flex;
      flex-direction: column;
    }
    .title{
      font-size: 4em;
      color: aliceblue;
      width: 100%;
      text-align: center;
      font-weight: bolder;
    }
    .area_box{
      width: auto;
      display: flex;
      justify-content: center;

    }
    .area{
      width: 900px;
      height: 400px;
      border-radius: 20px;
      font-size: 1.5em;

    }
    .user_name{
      font-size: 2em;
      color: azure;
    }
    .submit{
      width: 70%;
      height: 50px;
      border-radius: 25px;
      background-color: #e66465;
      color: azure;
      font-size: 2em;
      margin-top: 20px;
      margin-left: auto;
      margin-right: auto;
    }
    .content_box{
      width: 85%;
      margin: auto;
      height: auto;
    }
    .item{
      width: 100%;
      height: auto;
      border-radius: 20px;
      margin: 8px 0px 8px 0px;
      background-color: #9198e5;
      padding: 10px 10px 10px 30px;
    }

    .user_box{
      color: azure;
      font-size: 1.5em;

    }
    .content{
      color: azure;
      font-size: 1.5em;
    }
    .time_box{
      color: dimgray;
      font-size: 0.8em;
    }
    .delete{
      text-decoration: none;
      color: azure;
      font-weight: bold;
      margin-left: 20px;
      margin-top: 5px;
    }
    .delete:hover{
      color: blue;
    }
  </style>
</head>
<body>
<div class="main_box">
    <div class="box">
      <div class="title">留言板</div>
      <form class="submit_box">
        <div class="user_name">
          <%
            String username=(String)session.getAttribute("username");
            if (username==null){
              response.sendRedirect("messagelogin.jsp");
            }
            java.util.Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = format.format(date);
            String gender= (String) session.getAttribute("gender");
          %>
          欢迎用户：<% out.print(username);%>
        </div>
        <div class="area_box">
          <textarea rows="5" cols="40" placeholder="&emsp;发表新鲜事儿......" name="area" class="area"></textarea>
        </div>
        <button class="submit" >发表</button>
      </form>
      <div class="content_box">
      <%
        try {
          Connection conn = DobConn.getConn();
          session.setAttribute("username",username);
          Statement stat = conn.createStatement();
          String content = request.getParameter("area");
          if(content != null){
            String sql = "INSERT INTO messages(username,gender,content,time)VALUES('"+username+"','"+gender+"','"+content+"','"+dateStr+"')";
            try {
              stat.executeUpdate(sql);
            } catch (SQLException throwables) {
              throwables.printStackTrace();
            }
          }
          String sql1 = "SELECT * FROM messages order by time DESC";
          ResultSet rs = null;
          rs = stat.executeQuery(sql1);
          while(rs.next()){
            String name = rs.getString("username");
            String xingbie=rs.getString("gender");
            String r_content = rs.getString("content");
            String time = rs.getString("time");
            String num =rs.getString("num");
            out.print("<div class='item'><div class='inner_box'><div class='user_box'>用&emsp;户:" +name+" "+xingbie
                    + "</div><br><div class='content'>"
                    +r_content+"</div><br><div class='time_box'>"
                    +time+"</div><a class='delete' href='messagedelete.jsp?num="+num+"&name="+name+"'>"
                    + "删除</a><a class='delete' href='messagechange.jsp?num="+num+"&name="+name+"'>"
                    + "修改</a></div></div>");
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      %>
    </div>
  </div>
</div>
</body>
</html>
